package com.xiangyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyang.annotations.OperationLog;
import com.xiangyang.entity.Post;
import com.xiangyang.entity.dto.Result;
import com.xiangyang.entity.vo.PostInfoPage;
import com.xiangyang.mapper.IPostMapper;
import com.xiangyang.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

@Service
public class PostServiceImpl extends ServiceImpl<IPostMapper, Post> implements IPostService {
    @Autowired
    private IPostMapper postMapper;

    @Override
    @Transactional
    @OperationLog(title = "岗位管理", businessType = "添加岗位" ,newDataMethod = "getPostById" )
    public Result addPost(Post post) {
        // 检查岗位信息是否完整
        if (post == null || post.getPostName() == null || post.getPostCode() == null) {
            return Result.error("岗位信息不完整");
        }

        // 检查数据库中是否已存在相同的岗位编码
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_code", post.getPostCode());
        Long existingCount = postMapper.selectCount(queryWrapper);

        if (existingCount > 0) {
            return Result.error("岗位编码已存在");
        }

        // 插入岗位信息
        int insert = postMapper.insert(post);
        return insert == 1 ? Result.ok() : Result.error("岗位添加失败");
    }

    @Override
    @Transactional
    @OperationLog(title = "岗位管理", businessType = "删除岗位",oldDataMethod = "getPostById")
    public Result deletePost(Long postId) {
        int delete = postMapper.deleteById(postId);
        return delete == 1 ? Result.ok() : Result.error("岗位删除失败");
    }
    public Post getPostById(Long postId) {
        return postMapper.selectById(postId);
    }

    @Override
    public Result getPostList() {
        return Result.ok(postMapper.selectList(null));
    }

    @Override
    @Transactional
    @OperationLog(title = "岗位管理", businessType = "修改岗位",oldDataMethod = "getPostById" ,newDataMethod = "getPostById")
    public Result updatePostById(@RequestBody Post post) {
        if (post.getPostName() == null) {
            return Result.error("岗位名称为空");
        }
        if ( post.getPostCode() == null) {
            return Result.error("岗位编码为空");
        }
        if ( post.getPostStatus() == null) {
            return Result.error("岗位状态为空");
        }
        return postMapper.updateById(post) == 1 ? Result.ok() : Result.error("更新岗位失败");
    }

    @Override
    public Result<Object> getPostList(int pageNum, int pageSize) {
        if (pageNum <= 0 || pageSize <= 0) {
            return Result.error("参数不合法");
        }
        Page<Post> page = new Page<>(pageNum, pageSize);
        Page<Post> postPage = postMapper.selectPage(page, new QueryWrapper<>());
        return Result.ok(postPage);
    }

    @Override
    public Result selectPostInfo(PostInfoPage postInfoPage) {
        Page<Post> page = new Page<>(postInfoPage.getPage(), postInfoPage.getSize());
        LambdaQueryWrapper<Post> queryWrapper = new  LambdaQueryWrapper<>();
        queryWrapper.like(Post::getPostName, postInfoPage.getPostName());
        queryWrapper.like(Post::getPostStatus, postInfoPage.getPostStatus());
        return Result.ok(postMapper.selectPage(page, queryWrapper));
    }

}